{% extends "member/layout.html" %}

{% block title %}我的任务{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <!-- 页面标题和统计 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h3 class="fw-bold text-primary mb-1">
                    <i class="fas fa-tasks me-2"></i>我的任务
                </h3>
                <p class="text-muted mb-0">管理您在不同项目中的任务和进度</p>
            </div>
            <div class="d-flex align-items-center">
                <div class="me-3">
                    <span class="badge bg-primary bg-opacity-10 text-primary border border-primary border-opacity-25 rounded-pill px-3 py-2">
                        <i class="fas fa-list-check me-1"></i>总计: {{ tasks|length }}
                    </span>
                </div>
            </div>
        </div>

        <!-- 任务筛选和统计卡片 -->
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="card dashboard-card border-0 h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="flex-shrink-0">
                                <div class="stat-icon bg-primary bg-opacity-10 text-primary rounded-circle p-3">
                                    <i class="fas fa-list-check fs-4"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="card-title text-muted mb-1">全部任务</h5>
                                <h3 class="stat-number mb-0">{{ tasks|length }}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card dashboard-card border-0 h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="flex-shrink-0">
                                <div class="stat-icon bg-warning bg-opacity-10 text-warning rounded-circle p-3">
                                    <i class="fas fa-clock fs-4"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="card-title text-muted mb-1">待处理</h5>
                                <h3 class="stat-number mb-0">{{ tasks|selectattr('status', 'equalto', 'pending')|list|length }}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card dashboard-card border-0 h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="flex-shrink-0">
                                <div class="stat-icon bg-info bg-opacity-10 text-info rounded-circle p-3">
                                    <i class="fas fa-spinner fs-4"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="card-title text-muted mb-1">进行中</h5>
                                <h3 class="stat-number mb-0">{{ tasks|selectattr('status', 'equalto', 'in_progress')|list|length }}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card dashboard-card border-0 h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="flex-shrink-0">
                                <div class="stat-icon bg-success bg-opacity-10 text-success rounded-circle p-3">
                                    <i class="fas fa-check-circle fs-4"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="card-title text-muted mb-1">已完成</h5>
                                <h3 class="stat-number mb-0">{{ tasks|selectattr('status', 'equalto', 'completed')|list|length }}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 任务列表 -->
        <div class="card dashboard-card border-0">
            <!-- 标签页导航 -->
            <div class="card-header bg-transparent border-0 pb-0">
                <ul class="nav nav-tabs nav-tabs-custom border-0" role="tablist">
                    <li class="nav-item" role="presentation">
                        <a class="nav-link {% if status_filter == 'all' %}active{% endif %}"
                           href="{{ url_for('member.task.list_tasks', status='all') }}">
                            <i class="fas fa-list-check me-2"></i>全部
                            <span class="badge bg-primary bg-opacity-10 text-primary rounded-pill ms-2">{{ tasks|length }}</span>
                        </a>
                    </li>
                    <li class="nav-item" role="presentation">
                        <a class="nav-link {% if status_filter == 'pending' %}active{% endif %}"
                           href="{{ url_for('member.task.list_tasks', status='pending') }}">
                            <i class="fas fa-clock me-2"></i>待处理
                            <span class="badge bg-warning bg-opacity-10 text-warning rounded-pill ms-2">{{ tasks|selectattr('status', 'equalto', 'pending')|list|length }}</span>
                        </a>
                    </li>
                    <li class="nav-item" role="presentation">
                        <a class="nav-link {% if status_filter == 'in_progress' %}active{% endif %}"
                           href="{{ url_for('member.task.list_tasks', status='in_progress') }}">
                            <i class="fas fa-spinner me-2"></i>进行中
                            <span class="badge bg-info bg-opacity-10 text-info rounded-pill ms-2">{{ tasks|selectattr('status', 'equalto', 'in_progress')|list|length }}</span>
                        </a>
                    </li>
                    <li class="nav-item" role="presentation">
                        <a class="nav-link {% if status_filter == 'completed' %}active{% endif %}"
                           href="{{ url_for('member.task.list_tasks', status='completed') }}">
                            <i class="fas fa-check-circle me-2"></i>已完成
                            <span class="badge bg-success bg-opacity-10 text-success rounded-pill ms-2">{{ tasks|selectattr('status', 'equalto', 'completed')|list|length }}</span>
                        </a>
                    </li>
                </ul>
            </div>

            <div class="card-body">
                {% if tasks %}
                <div class="row">
                    {% for task in tasks %}
                    <div class="col-lg-6 col-xl-4 mb-4">
                        <div class="card task-card border-0 h-100">
                            <div class="card-body">
                                <!-- 任务头部 -->
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <div>
                                        <span class="badge status-badge bg-{{
                                            'warning' if task.status == 'pending'
                                            else 'info' if task.status == 'in_progress'
                                            else 'success'
                                        }} bg-opacity-10 text-{{
                                            'warning' if task.status == 'pending'
                                            else 'info' if task.status == 'in_progress'
                                            else 'success'
                                        }} border border-{{
                                            'warning' if task.status == 'pending'
                                            else 'info' if task.status == 'in_progress'
                                            else 'success'
                                        }} border-opacity-25 rounded-pill px-3 py-1">
                                            <i class="fas {{
                                                'fa-clock' if task.status == 'pending'
                                                else 'fa-spinner' if task.status == 'in_progress'
                                                else 'fa-check-circle'
                                            }} me-1"></i>
                                            {{ task.status_label }}
                                        </span>
                                    </div>
                                    <div class="dropdown">
                                        <button class="btn btn-sm btn-light rounded-circle" type="button" data-bs-toggle="dropdown">
                                            <i class="fas fa-ellipsis-v"></i>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-end">
                                            <li><a class="dropdown-item" href="{{ url_for('member.jira.project_detail', project_id=task.stage.project.id) }}">
                                                <i class="fas fa-eye me-2"></i>查看详情
                                            </a></li>
                                            <li><hr class="dropdown-divider"></li>
                                            <li><a class="dropdown-item text-warning" href="#">
                                                <i class="fas fa-flag me-2"></i>标记重要
                                            </a></li>
                                        </ul>
                                    </div>
                                </div>

                                <!-- 任务信息 -->
                                <div class="mb-3">
                                    <h5 class="fw-bold text-dark mb-2">{{ task.name }}</h5>
                                    <p class="text-muted small mb-0">任务ID: {{ task.id }}</p>
                                </div>

                                <!-- 项目信息 -->
                                <div class="d-flex align-items-center mb-3">
                                    <div class="flex-shrink-0">
                                        <div class="project-icon bg-primary bg-opacity-10 text-primary rounded-circle d-flex align-items-center justify-content-center">
                                            <i class="fas fa-folder-open"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1 ms-2">
                                        <h6 class="mb-0 fw-medium">{{ task.stage.project.name }}</h6>
                                        <small class="text-muted">所属项目</small>
                                    </div>
                                </div>

                                <!-- 阶段信息 -->
                                <div class="d-flex align-items-center mb-3">
                                    <div class="flex-shrink-0">
                                        <div class="stage-icon bg-secondary bg-opacity-10 text-secondary rounded-circle d-flex align-items-center justify-content-center">
                                            <i class="fas fa-layer-group"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1 ms-2">
                                        <h6 class="mb-0 fw-medium">{{ task.stage.name }}</h6>
                                        <small class="text-muted">项目阶段</small>
                                    </div>
                                </div>

                                <!-- 时间信息 -->
                                <div class="d-flex justify-content-between align-items-center text-muted small mb-3">
                                    <div>
                                        <i class="fas fa-calendar-day me-1"></i>
                                        {{ task.due_date.strftime('%Y-%m-%d') if task.due_date else '未设置' }}
                                    </div>
                                    <div class="text-{{ 'danger' if task.due_date and task.due_date < now else 'muted' }}">
                                        {% if task.due_date %}
                                            {% if task.due_date < now %}
                                            <i class="fas fa-exclamation-triangle me-1"></i>已过期
                                            {% else %}
                                            <i class="fas fa-clock me-1"></i>截止日期
                                            {% endif %}
                                        {% endif %}
                                    </div>
                                </div>

                                <!-- 操作按钮 -->
                                <div class="d-grid gap-2">
                                    {% if task.status == 'todo' %}
                                    <form action="{{ url_for('member.task.accept', task_id=task.id) }}" method="post" class="d-inline">
                                        <input type="hidden" name="project_id" value="{{ task.stage.project.id }}">
                                        <button type="submit" class="btn btn-success w-100 mb-2">
                                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                            <i class="fas fa-check-circle me-2"></i>接受任务
                                        </button>
                                    </form>
                                    {% endif %}
                                    <a href="{{ url_for('member.task.task_detail', task_id=task.id) }}"
                                       class="btn btn-outline-primary">
                                        <i class="fas fa-tasks me-2"></i>查看任务
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <!-- 空状态 -->
                <div class="text-center py-5">
                    <div class="mb-4">
                        <i class="fas fa-tasks text-muted" style="font-size: 4rem;"></i>
                    </div>
                    <h5 class="text-muted mb-3">暂无任务</h5>
                    <p class="text-muted mb-4">您当前没有分配到的任务，请等待项目负责人分配或联系管理员</p>
                    <a href="{{ url_for('member.dashboard') }}" class="btn btn-outline-primary">
                        <i class="fas fa-tachometer-alt me-2"></i>返回仪表盘
                    </a>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<style>
.task-card {
    transition: all 0.3s ease;
    border-left: 4px solid transparent;
}

.task-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1) !important;
    border-left-color: var(--primary-color);
}

.nav-tabs-custom .nav-link {
    border: none;
    border-radius: 8px;
    margin-right: 8px;
    padding: 0.75rem 1.25rem;
    color: #6c757d;
    font-weight: 500;
    transition: all 0.2s;
}

.nav-tabs-custom .nav-link.active {
    background-color: rgba(67, 97, 238, 0.1);
    color: var(--primary-color);
}

.nav-tabs-custom .nav-link:hover:not(.active) {
    background-color: rgba(108, 117, 125, 0.1);
}

.project-icon, .stage-icon {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-icon {
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.status-badge {
    font-weight: 500;
    font-size: 0.8rem;
}

.dropdown-toggle::after {
    display: none;
}

.btn-light.rounded-circle {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // 添加卡片悬停效果
    const taskCards = document.querySelectorAll('.task-card');

    taskCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.zIndex = '10';
        });

        card.addEventListener('mouseleave', function() {
            this.style.zIndex = '1';
        });
    });

    // 处理重要标记
    const importantButtons = document.querySelectorAll('.dropdown-item.text-warning');

    importantButtons.forEach(button => {
        button.addEventListener('click', function(e) {
            e.preventDefault();
            const card = this.closest('.task-card');
            card.style.borderLeftColor = '#ffc107';

            // 显示提示
            const toast = document.createElement('div');
            toast.className = 'toast align-items-center text-white bg-warning border-0 position-fixed bottom-0 end-0 m-3';
            toast.innerHTML = `
                <div class="d-flex">
                    <div class="toast-body">
                        <i class="fas fa-flag me-2"></i>已标记为重要任务
                    </div>
                    <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
                </div>
            `;
            document.body.appendChild(toast);

            const bsToast = new bootstrap.Toast(toast);
            bsToast.show();

            // 3秒后移除提示
            setTimeout(() => {
                toast.remove();
            }, 3000);
        });
    });

    // 处理过期任务提醒
    const overdueTasks = document.querySelectorAll('.text-danger');
    if (overdueTasks.length > 0) {
        const toast = document.createElement('div');
        toast.className = 'toast align-items-center text-white bg-danger border-0 position-fixed bottom-0 start-0 m-3';
        toast.innerHTML = `
            <div class="d-flex">
                <div class="toast-body">
                    <i class="fas fa-exclamation-triangle me-2"></i>您有 ${overdueTasks.length} 个任务已过期
                </div>
                <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
            </div>
        `;
        document.body.appendChild(toast);

        const bsToast = new bootstrap.Toast(toast);
        bsToast.show();

        // 5秒后移除提示
        setTimeout(() => {
            toast.remove();
        }, 5000);
    }
});
</script>
{% endblock %}